package com.tensquare.user.service;

import com.tensquare.user.dao.AdminDao;
import com.tensquare.user.pojo.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import utils.IdWorker;

@Service
public class AdminService {
    @Autowired
    private AdminDao adminDao;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private BCryptPasswordEncoder passwordEncoder;
    //1.添加管理员
    public void add(Admin admin) {
        admin.setId(idWorker.nextId()+"");
        admin.setPassword(passwordEncoder.encode(admin.getPassword()));
        adminDao.save(admin);
    }
    //2.管理员登录
    public Admin isTrue(Admin admin) {
        //2.1先根据用户名得到对应的管理员信息,再判断密码是否正确
        Admin loginAdmin = adminDao.findByLoginname(admin.getLoginname());
        if (loginAdmin!=null && passwordEncoder.matches(admin.getPassword(),loginAdmin.getPassword())){
            return loginAdmin;
        }
        return null;
    }
    //3.删除管理员
    public void delete(String adminId) {
        adminDao.deleteById(adminId);
    }
}
